有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JMS主题生存时间

我正在开发一个由一些模块组成的应用程序。在其中一个模块中,有人创建了一个主题生产者,该生产者发布关于某个主题的消息,但该模块没有一个主题消费者来将消息出列。主题制作人使用setTimeToLive()将生存时间属性设置为300000毫秒

我预计,如果没有消费者,消息将在30万毫秒内过期,并被解除分配

该应用程序部署在Tomcat 6.0.36上,并使用外部ActiveMQ服务器来处理队列和主题

使用Java VisualVM监控ActiveMQ在MBeans选项卡的主题设置下,我看到变量“排队计数”增加,但我不知道生存时间设置是否对这些消息生效。我希望计数器“ExpiredCount”增加,但它仍然固定为0

有没有办法了解这些消息是否仍保留在内存中,或者它们是否被释放

非常感谢

编辑:

我在netbeans 7.3上使用j2ee教程示例进行了一些测试,使用内部glassfish 3.1作为服务器,使用jvisualvm对其进行监控,所有工作如api所述:

The JMS API provides no mechanism for browsing a topic. Messages usually disappear from a topic as soon as they appear: if there are no message consumers to consume them, the JMS provider removes them. Although durable subscriptions allow messages to remain on a topic > while the message consumer is not active, no facility exists for examining them.

我读到glassfish在activeMQ内部使用,所以我希望这对独立的activeMQ服务器也是有效的

结束编辑


共 (1) 个答案

  1. # 1 楼答案

    引用Creating Robust JMS Applications的一段话:

    5.1.4 Allowing Messages to Expire
    [...]
    When the message is published, the specified timeToLive is added to the current time to give the expiration time. Any message not delivered before the specified expiration time is destroyed.

    另一段引用自javax.jms.Message#getJMSExpiration()的来源:

    When a message's expiration time is reached, a provider should discard it. [...]
    Clients should not receive messages that have expired; however, the JMS API does not guarantee that this will not happen.

    因此,对于非持久订户:

    1. 服务器将消息发送给每个连接的订户。消息中的到期值设置为“当前时间+TTL”。断开连接的订阅者将不会收到任何信息
    2. 如果消息尚未过期,连接的客户端将正常接收消息
    3. 如果连接的客户端在接收消息之前花费的时间太长(消息已过期),则服务器可能会丢弃该消息(可能是在服务器尚未将其推入客户端缓冲区的情况下),或者可以接收该消息(可能是在消息已在客户端缓冲区中的情况下)

    因此,在您的情况下,如果没有消费者,消息可能根本不会被存储排队计数增加,过期计数保持为0过期计数只应在连接(但空闲)订户的情况下增加

    引用How do I find the Size of a Queue

    Enqueue Count - the total number of messages sent to the queue since the last restart
    Expired Count - the number of messages that were not delivered because they were expired

    注意:使用JBoss 7的测试表明,在这种情况下,消息不会出现在客户端